package com.kh.pajx.sop.service.order.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kh.pajx.sop.base.BaseServiceImpl;
import com.kh.pajx.sop.dao.order.IReplaceCardOrderDao;
import com.kh.pajx.sop.dao.order.IReplaceCardOrderRelationDao;
import com.kh.pajx.sop.domain.order.ReplaceCardOrder;
import com.kh.pajx.sop.domain.order.ReplaceCardOrderRelation;
import com.kh.pajx.sop.service.order.IReplaceCardOrderService;
import com.kh.pajx.sop.util.common.FormatDate;
import com.kh.pajx.sop.util.common.ValidatorUtils;
import com.kh.pajx.sop.util.page.QueryHelper;

/**
 * 补卡单据业务逻辑实现类
 * @author:	azzcsimp
 * @Createdate:	2014年11月11日 上午10:26:50
 */
@Service
@Transactional
public class ReplaceCardOrderServiceImpl extends BaseServiceImpl<ReplaceCardOrder> implements IReplaceCardOrderService {

	@Resource
	private IReplaceCardOrderDao replaceCardOrderDao;
	
	@Resource
	private IReplaceCardOrderRelationDao replaceCardOrderRelationDao;
	
	@Override
	public void waits(ReplaceCardOrder model, int pageNum, int numPerPage) throws Exception {
		QueryHelper queryHelper = new QueryHelper(ReplaceCardOrder.class, "o");
		if (StringUtils.isNotEmpty(StringUtils.trim(model.getSclName()))) {
			queryHelper.addCondition("o.sclName like ? ", "%" + StringUtils.trim(model.getSclName()) + "%");
		}
		
		if (StringUtils.isNotEmpty(StringUtils.trim(model.getRcoNo()))) {
			queryHelper.addCondition("o.rcoNo=?", StringUtils.trim(model.getRcoNo()));
		}
		
		if (model.getPayStatus() != null) {
			if (model.getPayStatus() == 0) {
				queryHelper.addCondition("o.payStatus=?", model.getPayStatus());
			} else if (model.getPayStatus() == 2) {
				queryHelper.addCondition("o.payStatus=?", model.getPayStatus());
			}
		}
		queryHelper.addCondition("o.progStatus=?", 2);
		queryHelper.addCondition("o.rcoStatusFlag=?", 1);
		queryHelper.addOrderProperty("o.createTime", false);
		queryHelper.preparePageBean(replaceCardOrderDao, pageNum, numPerPage);
	}
	
	@Override
	public void already(ReplaceCardOrder model, int pageNum, int numPerPage) throws Exception {
		QueryHelper queryHelper = new QueryHelper(ReplaceCardOrder.class, "o");
		if (StringUtils.isNotEmpty(StringUtils.trim(model.getSclName()))) {
			queryHelper.addCondition("o.sclName like ? ", "%" + StringUtils.trim(model.getSclName()) + "%");
		}
		if (model.getProgStatus() != null) {
			if (model.getProgStatus() > -1) {
				queryHelper.addCondition("o.progStatus=?", model.getProgStatus());
			} else {
				queryHelper.addCondition("(o.progStatus>? or o.progStatus=?)", 2, 0);
			}
		} else {
			queryHelper.addCondition("(o.progStatus>? or o.progStatus=?)", 2, 0);
		}
		if (StringUtils.isNotEmpty(StringUtils.trim(model.getRcoNo()))) {
			queryHelper.addCondition("o.rcoNo=?", StringUtils.trim(model.getRcoNo()));
		}
		queryHelper.addCondition("o.rcoStatusFlag=?", 1);
		queryHelper.addOrderProperty("o.createTime", false);
		queryHelper.preparePageBean(replaceCardOrderDao, pageNum, numPerPage);
	}
	
	@Override
	public void changeList(ReplaceCardOrder model, int pageNum, int numPerPage) throws Exception {
		QueryHelper queryHelper = new QueryHelper(ReplaceCardOrder.class, "o");
		if (StringUtils.isNotEmpty(StringUtils.trim(model.getRcoNo()))) {
			queryHelper.addCondition("o.rcoNo=?", StringUtils.trim(model.getRcoNo()));
		}
		queryHelper.addCondition("o.payStatus=?", 1);
		queryHelper.addCondition("o.progStatus=?", 1);
		queryHelper.addCondition("o.rcoStatusFlag=?", 1);
		queryHelper.addOrderProperty("o.createTime", false).preparePageBean(replaceCardOrderDao, pageNum, numPerPage);
	}

	@Override
	public void change(String rcoId, String userId, String userName, String buyerEmail, String tradeNo)
			throws Exception {
		if (!ValidatorUtils.validateUUID(rcoId, false)) {
			throw new Exception("非法参数请求操作！");
		}
		ReplaceCardOrder rpCard = replaceCardOrderDao.getById(rcoId);
		if (rpCard == null) {
			throw new Exception("该订单未找！");
		}
		if (rpCard.getRcoStatusFlag() != 1) {
			throw new Exception("该订单未找到或已被删除！");
		}
		if (rpCard.getProgStatus() == 2) {
			throw new Exception("该订单无需手动确认收款！");
		}

		ReplaceCardOrderRelation upRel = replaceCardOrderRelationDao.getMarryRelationByRcoId(rcoId);
		if (null == upRel) {
			throw new Exception("无法获取单据" + rcoId + "扩展信息！");
		}

		// ==> 1.更新申请时间  (此处必须先修改主表)
		rpCard.setCreateTime(FormatDate.dateTimeDetail());
		replaceCardOrderDao.update(rpCard);

		// ==> 2.更新扩展信息
		upRel.setRelationStatusFlag(0);
		replaceCardOrderRelationDao.update(upRel);

		// ==> 3.新增扩展信息
		ReplaceCardOrderRelation saveRel = new ReplaceCardOrderRelation();
		saveRel.setRcoId(rcoId);
		saveRel.setProgStatus(2);
		saveRel.setPayStatus(2);
		saveRel.setDealTime(FormatDate.dateTimeDetail());
		saveRel.setDealOperId(userId);
		saveRel.setDealOperName(userName);
		saveRel.setRemark("已付款成功并提交审核。");
		saveRel.setRelationStatusFlag(1);
		saveRel.setBuyerEmail(buyerEmail);
		saveRel.setTradeNo(tradeNo);
		replaceCardOrderRelationDao.save(saveRel);

	}
	
	
	
	
	
	
	
	

	

	

	

	@Override
	public void filing(ReplaceCardOrder model, int pageNum, int numPerPage) throws Exception {
		QueryHelper queryHelper = new QueryHelper(ReplaceCardOrder.class, "o");
		if (StringUtils.isNotEmpty(StringUtils.trim(model.getSclName()))) {
			queryHelper.addCondition("o.sclName like ? ", "%" + StringUtils.trim(model.getSclName()) + "%");
		}
		if (model.getProgStatus() != null) {
			if (model.getProgStatus() > -1) {
				queryHelper.addCondition("o.progStatus=?", model.getProgStatus());
			} else {
				queryHelper.addCondition("(o.progStatus=? or o.progStatus=?)", 4, 5);
			}
		}
		if (StringUtils.isNotEmpty(StringUtils.trim(model.getRcoNo()))) {
			queryHelper.addCondition("o.rcoNo=?", StringUtils.trim(model.getRcoNo()));
		}
		queryHelper.addCondition("o.rcoStatusFlag=?", 3);
		queryHelper.addOrderProperty("o.createTime", false).preparePageBean(replaceCardOrderDao, pageNum, numPerPage);

	}

	

	// ==================================================

	@Override
	public ReplaceCardOrder getById(String uuid) {
		return replaceCardOrderDao.getById(uuid);
	}

	@Override
	public void update(ReplaceCardOrder demo) {
		replaceCardOrderDao.update(demo);
	}

	@Override
	public List<ReplaceCardOrder> getOrderList(Map param) {
		StringBuilder sb = new StringBuilder("");
		sb.append("select o from ReplaceCardOrder o , HsSchoolInfo scl where o.sclId=scl.sclId  ");
		String sclName = (String) param.get("sclName");
		String areaCode = (String) param.get("areaCode");
		String countyCode = (String) param.get("countyCode");
		String startDate = (String) param.get("startDate");
		String endDate = (String) param.get("endDate");
		if (StringUtils.isNotBlank(sclName)) {
			sb.append(" and o.sclName='").append(sclName).append("' ");
		}
		if (StringUtils.isNotBlank(areaCode)) {
			sb.append(" and scl.areaCode='").append(areaCode).append("' ");
		}
		if (StringUtils.isNotBlank(countyCode)) {
			sb.append(" and scl.countyCode='").append(countyCode).append("' ");
		}
		if (StringUtils.isNotBlank(startDate)) {
			sb.append(" and o.dealTime>='").append(startDate.replace(" ", "").replace(":", "").replace("-", ""))
					.append("' ");
		}
		if (StringUtils.isNotBlank(endDate)) {
			sb.append(" and o.dealTime<='").append(endDate.replace(" ", "").replace(":", "").replace("-", ""))
					.append("' ");
		}
		sb.append(" and o.progStatus>=4");
		sb.append(" order by o.sclName");
		System.out.println(sb.toString());
		return replaceCardOrderDao.getOrderList(sb.toString());
	}

	@Override
	public List<List<String>> getCardCount(Map param, String flag) {
		StringBuilder sb = new StringBuilder("");
		if ("1".equals(flag)) {//查询补卡总数
			sb.append("select nvl(sum(o.rco_count_number),0)");
		} else if ("2".equals(flag)) {//查询补卡总金额
			sb.append("select nvl(sum(o.rco_amount),0)");
		} else if ("3".equals(flag)) {//查询订单总数
			sb.append("select count(*)");
		} else if ("4".equals(flag)) {//收费的单据数
			sb.append("select count(*)");
		} else if ("5".equals(flag)) {//
			sb.append("select count(*)");
		}

		sb.append(" from hs_replace_card_order o , hs_school_info scl where o.scl_id=scl.scl_id  ");
		String sclName = (String) param.get("sclName");
		String areaCode = (String) param.get("areaCode");
		String countyCode = (String) param.get("countyCode");
		String startDate = (String) param.get("startDate");
		String endDate = (String) param.get("endDate");
		if (StringUtils.isNotBlank(sclName)) {
			sb.append(" and o.scl_name='").append(sclName).append("' ");
		}
		if (StringUtils.isNotBlank(areaCode)) {
			sb.append(" and scl.area_code='").append(areaCode).append("' ");
		}
		if (StringUtils.isNotBlank(countyCode)) {
			sb.append(" and scl.county_code='").append(countyCode).append("' ");
		}
		if (StringUtils.isNotBlank(startDate)) {
			sb.append(" and o.deal_time>='").append(startDate.replace(" ", "").replace(":", "").replace("-", ""))
					.append("' ");
		}
		if (StringUtils.isNotBlank(endDate)) {
			sb.append(" and o.deal_time<='").append(endDate.replace(" ", "").replace(":", "").replace("-", ""))
					.append("' ");
		}
		if ("4".equals(flag)) {
			sb.append(" and o.rco_amount>0");
		} else if ("5".equals(flag)) {
			sb.append(" and o.rco_amount=0");
		}
		sb.append(" and o.prog_status>=4");
		return replaceCardOrderDao.getByJdbcSQL(sb.toString());
	}

	@Override
	public List<List<String>> getAmount(Map param) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder("");
		sb.append("select sum(o.rco_amount) from hs_replace_card_order o , hs_school_info scl where o.scl_id=scl.scl_id  ");
		String sclName = (String) param.get("sclName");
		String areaCode = (String) param.get("areaCode");
		String countyCode = (String) param.get("countyCode");
		if (StringUtils.isNotBlank(sclName)) {
			sb.append(" and o.scl_name='").append(sclName).append("' ");
		}
		if (StringUtils.isNotBlank(areaCode)) {
			sb.append(" and scl.area_code='").append(areaCode).append("' ");
		}
		if (StringUtils.isNotBlank(countyCode)) {
			sb.append(" and scl.county_code='").append(countyCode).append("' ");
		}
		return replaceCardOrderDao.getByJdbcSQL(sb.toString());
	}

}
